package com.dragon.concurrent.demo.sleep;

/**
 * 当一个正在执行的线程调用sleep方法,该线程的状态为定时等待状态TIMED_WAITING
 *
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/6/1 17:00
 */
public class ThreadStateTest {

    public static void main(String[] args) throws InterruptedException {
        Thread1 t2 = new Thread1();
        Thread1 t3 = new Thread1();
        t2.start();
        t3.start();
        Thread.sleep(6000L);
        System.out.println("t2线程的状态:" + t2.getState());
    }
}

class Thread1 extends Thread {

    Thread2 thread2;

    public Thread1(Thread2 thread2) {
        this.thread2 = thread2;
    }

    public Thread1() {

    }

    @Override
    public synchronized void run() {
        System.out.println(Thread.currentThread().getName() + "进入");
        for (int i = 0; i < 5; i++) {
            try {
                System.out.println("线程的名称" + Thread.currentThread().getName() + ",状态" + getState());
                System.out.println();

                //为了减少打印次数，所以t1每打印一次睡1秒
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
        }
    }
}

class Thread2 extends Thread {
    @Override
    public void run() {
        System.out.println("进入t2线程，马上进入睡眠");
        try {
            //睡眠5秒钟。
            sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("t2睡眠结束");
    }
}
